package org.apache.learning.provider;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.builder.annotation.ProviderContext;

import java.util.List;

/**
 * @author HanQi [Jpanda@aliyun.com]
 * @version 1.0
 * @since 2020/5/19 17:15:43
 */
public interface UserMapper {

    @SelectProvider(type = SqlProvider.class, method = "selectUserByName")
    List<User> selectUserByNameWithProvider(@Param("name") String name);

    @SelectProvider(type = SqlProvider.class, method = "selectUserByName2")
    List<User> selectUserByNameWithProviderContext(@Param("name") String name);

    @Slf4j
    class SqlProvider {
        public String selectUserByName() {
            return "SELECT * FROM USER WHERE name= #{name}";
        }
        public String selectUserByName2(ProviderContext context) {
            log.info("调用方的的类型:{}", context.getMapperType().getSimpleName());
            log.info("调用方的的方法:{}", context.getMapperMethod().getName());
            return "SELECT * FROM USER WHERE name= #{name}";
        }
    }

    @Select("SELECT * FROM USER WHERE name= #{name}")
    List<User> selectUserByNameWithSimpleAnnotation(@Param("name") String name);

    List<User> selectUserByNameWithXML(@Param("name") String name);

}